/*
 * @description: 
 * @Author: Islands
 * @Date: 2023-11-13 13:13:54
 * @LastEditTime: 2024-03-25 21:59:53
 */
import { defineStore } from 'pinia';
import { ITabs } from '@/components/tag-views/index.d';
export const useTagViewsStore = defineStore('tags', {
    state: () => {
        return {
            /**
             * 当前激活的选项卡，他跟当前激活的路由是一直的
             */
            activeTab:'' as string,
            tabList: <Array<ITabs>>[],
            contentId: '' as string,
        }
    },
    actions: {
        /**
         * 修改activeTab值
         * @param route 
         */
        setActiveTab(route:string){
            this.activeTab=route
        },
        /**
         * 添加/修改 tabs
         * @param tab 
         * @returns 
         */
        setTbaList(tab: ITabs) {
            // console.log(Object.prototype.toString.call(this.tabList));//判断类型
            //判断是否已经存在，如果不存在才放入
            if (this.tabList.some((item: { path: any; }) => item.path === tab.path)) return;
            this.tabList.push(tab);
        },
        /**
         * 单个移除
         * @param index 
         */
        removeTabList(targetName: string, cb: (arg: number) => void) {
            const index = this.tabList.findIndex((item: any) => item.path == targetName);
            this.tabList.splice(index, 1);
            cb(index - 1)
        },

        /**
         * 右键打开菜单保存path
         * @param contextMenuTabId 
         */
        setContextTabId(tabId: string) {
            this.contentId = tabId;
        },

        /**
         * 关闭所有tabs
         */
        closeAllTabs() {
            this.tabList = [];
        },

        /**
         * 关闭其他，左边，右边
         * @param par 
         * @param cb 
         */
        closeOtherTabs(par: string, cb: (arg: string) => void) {
            if (par == 'other') {
                this.tabList = this.tabList.filter((item: any) => item.path == this.contentId);
                cb(this.contentId)
            } else if (par == 'left') {
                let index = this.tabList.findIndex((item: any) => item.path == this.contentId);   //获取索引 
                this.tabList.splice(0, index);
                cb(this.contentId)
            } else if (par == 'right') {
                let rightIndex = this.tabList.findIndex((item: any) => item.path == this.contentId);   //获取索引  
                this.tabList.splice(rightIndex + 1);
                cb(this.contentId)
            }
        },
    },
    getters: {
        getTabList(state: { tabList: Array<ITabs> }) { return state.tabList; },
        getContentId(state: { contentId: string }) { return state.contentId; },
    },
    persist: {
        storage:sessionStorage
    }
})